package com.canbot.u05.control;


import com.canbot.u05.api.ReceiveDataInterface;
import com.canbot.u05.utils.Logger;

/**
 *
 *
 */

public class RobotMgr {

        private static final String TAG = "RobotMgr";

        private static RobotMgr instance = null;

        private SocketClient mSocketClient = null;

        private RobotMgr() {
                mSocketClient = new SocketClient();
        }


        public void init(ReceiveDataInterface listener) {
                mSocketClient.init(listener);
                mSocketClient.startProcess();
        }


        /**
         * 返回 true 表示连接socket 成功，false则是失败.
         *
         * @return
         */
        public boolean connectToServer() {
                return mSocketClient.connectToServer(null);
        }


        public static RobotMgr getInstance() {
                if (instance == null) {
                        synchronized (RobotMgr.class) {
                                if (instance == null) {
                                        instance = new RobotMgr();
                                }
                        }
                }
                return instance;
        }

        /**
         * 打开地图
         */
        public void openMap() {
                Logger.d(TAG, "--- openMap ---");
                mSocketClient.sendMessage(EncodeUtil.mapSwitch((byte) 0x00));
        }

        /**
         * 关闭地图
         */
        public void closeMap() {
                Logger.d(TAG, "--- closeMap ---");
                mSocketClient.sendMessage(EncodeUtil.mapSwitch((byte) 0x01));
        }

        /**
         * 执行动作库里面的动作
         */
        public void exeActionLib(int actionID) {
                Logger.d(TAG, "--- exeActionLib --- actionID=" + actionID);
                mSocketClient.sendMessage(EncodeUtil.amrSportControl(actionID));
        }

        /**
         * 键盘控制
         */
        private static final short moveSpeed = 300; // 默认的移动速度

        private static final short rotateSpeed = 10; // 默认的转动速度

        public void keyControl(int key) {
                Logger.d(TAG, "--- keyControl ---key=" + key);
//        mSocketClinet.sendMessage(EncodeUtil.keyControl(key));
                if (Cmd.Key_qianjin == key) {
                        openSportControl(moveSpeed, (short) 0, (short) 0);
                }
                else if (Cmd.Key_houtui == key) {
                        openSportControl((short) (-moveSpeed), (short) 0, (short) 0);
                }
                else if (Cmd.Key_youzhuan == key) {
                        openSportControl((short) 0, (short) 0, rotateSpeed);
                }
                else if (Cmd.Key_zuozhuan == key) {
                        openSportControl((short) 0, (short) 0, (short) (-rotateSpeed));
                }
                else if (Cmd.Key_tingzhi == key) {
                        openSportControl((short) 0, (short) 0, (short) 0);
                }
        }

        public void keyControl(short moveSpeed,int key) {
                Logger.d(TAG, "--- keyControl ---key=" + key);
                //        mSocketClinet.sendMessage(EncodeUtil.keyControl(key));
                if (Cmd.Key_qianjin == key) {
                        openSportControl(moveSpeed, (short) 0, (short) 0);
                }
                else if (Cmd.Key_houtui == key) {
                        openSportControl((short) (-moveSpeed), (short) 0, (short) 0);
                }
                else if (Cmd.Key_youzhuan == key) {
                        openSportControl((short) 0, (short) 0, rotateSpeed);
                }
                else if (Cmd.Key_zuozhuan == key) {
                        openSportControl((short) 0, (short) 0, (short) (-rotateSpeed));
                }
                else if (Cmd.Key_tingzhi == key) {
                        openSportControl((short) 0, (short) 0, (short) 0);
                }
        }


        /**
         * 头部控制
         */
        public void headControl(int joint, short angle) {
                Logger.d(TAG, "--- headControl ---joint=" + joint + " angle=" + angle);
                mSocketClient.sendMessage(EncodeUtil.jointControl(joint, angle));
        }

        /**
         * 开放运动控制
         */
        public void openSportControl(short xSpeed, short ySpeed, short rotateSpeed) {
                Logger.d(TAG, "--- openSportControl ---xSpeed=" + xSpeed + " ySpeed=" + ySpeed + "  rotateSpeed=" + rotateSpeed);
                mSocketClient.sendMessage(EncodeUtil.openSportControl(xSpeed, ySpeed, rotateSpeed));
        }

        /**
         * 发送透传数据
         */
        public void sendTouChuan(int msgType, String data) {
                Logger.d(TAG, "--- sendTouChuan ---msgType=" + msgType + " data=" + data);
                mSocketClient.sendMessage(EncodeUtil.sendTouChuan(msgType, data));
        }

}
